﻿#include "audio/wavegen.h"

using namespace std;

bool anip::audio::HarmonicOscillator::Instance::read(
	double* dest, 
	unsigned int nSamples, 
	unsigned int start)
{
	double v0 = 2 * 3.14159 * _freq;
	for (unsigned int i = 0; i < nSamples; i++)
	{
		double val = 0;
		double v1 = ((double)start + i) / _freq;
		for (const Harmonic& h : _osc.harmonics)
		{
			double fac = h.octave < 0 ? 1 / h.octave : h.octave;
			val += sin((v1 + h.timeOffset) * v0 * fac) * h.amplitude;
		}
		for (int j = 0; j < _osc._channels; j++)
			dest[i * _osc._channels + j] = val;
	}
	return start > _length * _osc._sampleRate;
}